package com.ruoyi.web.controller.his;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.his.domain.vo.HisNurseExcelVo;
import com.ruoyi.his.domain.vo.HisNurseVo;
import com.ruoyi.his.domain.vo.OdsDoctorAllVo;
import com.ruoyi.his.domain.vo.OdsListDoctorAllVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.his.domain.HisNurse;
import com.ruoyi.his.service.IHisNurseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 * 
 * @author Alan
 * @date 2024-03-24
 */
@RestController
@RequestMapping("/his/nurse")
public class HisNurseController extends BaseController
{
    @Autowired
    private IHisNurseService hisNurseService;

    @Autowired
    private RedisCache redisCache;
    @PostMapping("/insterNurse")
    public AjaxResult insterNurse(@RequestBody HisNurseVo vo){
        return hisNurseService.insertNure(vo);
    }

    /**
     * 护士注册的业务
     * @param hisNurse
     * @return
     */
    @PostMapping("/registerNurse")
    public AjaxResult registerNurse(@RequestBody HisNurse hisNurse)
    {
        if(hisNurse.getReCount()==null){
            return AjaxResult.error("系统繁忙，稍后再试！");
        }
//        //如果有重复的记录，则调用非选择护士的注册方式
//        if(hisNurse.getReCount()>0){
//            return hisNurseService.registerNurseUnSelect(hisNurse);
//        }
        //没有重复的医院名称，调用选择护士的注册方式
        return hisNurseService.registerNurseSelect(hisNurse);

    }

    /**
     * 通过签名判断医院名称是否重复
     * @param signature
     * @return
     */
    @GetMapping("/checkSameName")
    public int checkSameName(@RequestParam(required = false) String signature ){
        return hisNurseService.checkSameName(signature);
    }

    /**
     * 通过签名获取医生
     * @param signature
     * @return
     */
    @GetMapping("/getNurListBySig")
    public List<HisNurse> getNurListBySig(@RequestParam(required = false) String signature ){
        return hisNurseService.selectOdsNurListBySig(signature);
    }


    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('his:nurse:list')")
    @GetMapping("/list")
    public TableDataInfo list(HisNurse hisNurse)
    {
        startPage();
        List<String> hospitalIds =new ArrayList<>();
        List<String> signatures =new ArrayList<>();
        List<Map> mapList = redisCache.getCacheList("hospitalId");
        for (Map map :mapList){
            if(map.get("hospitalId")!=null){
                hospitalIds.add(map.get("hospitalId").toString());
            }
            if(map.get("signature")!=null){
                signatures.add(map.get("signature").toString());
            }
        }
        List<HisNurse> list = hisNurseService.selectHisNurseList(hisNurse);
        return getDataTable(list);
    }
    @GetMapping("/nurTowlist")
    public TableDataInfo nurTowlist(HisNurse hisNurse)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<HisNurse> list = hisNurseService.selectnurTowlist(hisNurse);
        int num = list.size();
        list=list.stream().skip((pageNum -1) * pageSize).limit(pageSize).collect(Collectors.toList());
        TableDataInfo rsp = new TableDataInfo();
        rsp.setCode(HttpStatus.SUCCESS);
        rsp.setRows(list);
        rsp.setTotal(num);
        return rsp;
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('his:nurse:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HisNurse hisNurse)
    {
        List<HisNurse> list = hisNurseService.selectHisNurseList(hisNurse);
        ExcelUtil<HisNurse> util = new ExcelUtil<HisNurse>(HisNurse.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }



    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<HisNurseExcelVo> util = new ExcelUtil<HisNurseExcelVo>(HisNurseExcelVo.class);
        return util.importTemplateExcel("护士导入");
    }
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<HisNurseExcelVo> util = new ExcelUtil<HisNurseExcelVo>(HisNurseExcelVo.class);
        List<HisNurseExcelVo> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = hisNurseService.importNurData(userList, updateSupport, operName);
//        String message = hisNurseService.importData(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('his:nurse:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(hisNurseService.selectHisNurseById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('his:nurse:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HisNurse hisNurse)
    {
        return toAjax(hisNurseService.insertHisNurse(hisNurse));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('his:nurse:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HisNurse hisNurse)
    {
        return toAjax(hisNurseService.updateHisNurse(hisNurse));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('his:nurse:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hisNurseService.deleteHisNurseByIds(ids));
    }
}
